﻿package net.wangit.demo.unit;

import net.wangit.Wangit;
import net.wangit.Unit;
import net.wangit.context.ActionContext;
import net.wangit.jdbc.DB;
import net.wangit.jdbc.DBFactory;
import net.wangit.jdbc.QueryParameters;
import net.wangit.support.UnitInterceptor;

public class TablesQueryUI implements UnitInterceptor {

	public String intercept(ActionContext ac, Unit unit) throws Exception {
		String sql = "select t1.cdid,t1.cdname,t1.artist,t2.id,t2.musicname from CD t1,MUSIC t2 where t1.cdid=t2.cdid";
		DB db = DBFactory.getDB("demo");
		// 设置CD表查询条件
		sql = Wangit.addConditionAndOrder(ac, db, sql, "demo",
				"query.tables.query", "CD", "t1");
		// 设置MUSIC表查询条件
		sql = Wangit.addConditionAndOrder(ac, db, sql, "demo",
				"query.tables.query", "MUSIC", "t2");
		// 获得分页参数
		QueryParameters qp = Wangit.getQueryParameters(ac, db, sql, "demo",
				"query.tables.list");
		unit.setData(db.query(sql, qp));
		unit
				.setContent("<br>参见单元“query.tables”、UI“net.wangit.demo.unit.TablesQueryUI”，自动设置单元的条件，自动设置分页参数。<br><br>最终执行的sql语句：<br><div style='border:1px solid #dddddd;padding:5px;'>"
						+ sql + "</div>");
		return unit.write(ac);
	}

}
